package com.zzyl.nursing.controller;


import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDTO;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.ReservationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;

import java.util.List;


/**
 * 预约信息Controller
 *
 * @author bear_tianyu
 * @date 2025-02-27
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
public class ReservationController extends BaseController {
    @Autowired
    private IReservationService reservationService;

    /**
     * 查询预约信息列表
     */
//    @PreAuthorize("@ss.hasPermi('member:reservation:list')")
    @GetMapping("/page")
    @ApiOperation(value = "获取预约信息列表")
    public TableDataInfo<List<Reservation>> list(Reservation reservation) {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return getDataTable(list);
    }

    /**
     * 获取预约信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('member:reservation:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取预约信息详细信息")
    public R<Reservation> getInfo(@ApiParam(value = "预约信息ID", required = true)
                                  @PathVariable("id") Long id) {
        return R.ok(reservationService.selectReservationById(id));
    }

    /**
     * 新增预约信息
     */
//    @PreAuthorize("@ss.hasPermi('member:reservation:add')")
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增预约信息")
    public AjaxResult add(@ApiParam(value = "预约信息实体", required = true) @RequestBody ReservationDTO reservationDTO) {
        return toAjax(reservationService.insertReservation(reservationDTO));
    }

    /**
     * 修改预约信息
     */
//    @PreAuthorize("@ss.hasPermi('member:reservation:edit')")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping("{id}/cancel")
    @ApiOperation("修改预约信息")
    public AjaxResult edit(@ApiParam(value = "预约信息ID", required = true)
                           @PathVariable("id") Long id) {
        return toAjax(reservationService.updateReservation(id));
    }

    /**
     * 删除预约信息
     */
//    @PreAuthorize("@ss.hasPermi('member:reservation:remove')")
    @Log(title = "预约信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除预约信息")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids) {
        return toAjax(reservationService.deleteReservationByIds(ids));
    }

    /**
     * 查询当天取消预约数量
     *
     * @return
     */
    @GetMapping("cancelled-count")
    public AjaxResult cancelledCount() {
        int count = reservationService.cancelledCount();
        return success(count);
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time
     * @return
     */
    @GetMapping("countByTime")
    public R countByTime(String time) {
        List<ReservationVO> reservations = reservationService.countByTime(time);
        return R.ok(reservations);
    }
}
